package com.itheima.web;

import com.itheima.R;
import com.itheima.pojo.UmsResource;
import com.itheima.service.UmsResourceService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-11:57
 */
@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;


    /**
     * 添加新资源
     */
    @PostMapping("/create")
    public R createResource(@RequestBody UmsResource umsResource){
        umsResourceService.create(umsResource);
        return R.success(null);
    }


    /**
     * 根据id删除资源
     */
    @PostMapping("/delete/{id}")
    public R deleteResourceById(@PathVariable("id") Long id){
        umsResourceService.delete(id);
        return R.success(null);
    }


    /**
     * 分页模糊查询后台资源
     */
    @GetMapping("/list")
    public R list(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword){
        PageResult pageInfo = umsResourceService.pageList(categoryId, nameKeyword, pageNum, pageSize, urlKeyword);
        return R.success(pageInfo);
    }


    /**
     * 查询所有后台资源
     */
    @GetMapping("/listAll")
    public R listAll(){
        List<UmsResource> resourceList = umsResourceService.listAll();
        return R.success(resourceList);
    }


    /**
     * 修改后台资源
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable("id") Long id, @RequestBody UmsResource resource){
        umsResourceService.update(id, resource);
        return R.success(null);
    }


    /**
     * 根据id获取资源详情
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        UmsResource resource = umsResourceService.getById(id);
        return R.success(resource);
    }
}
